// 往来记录
import {queryRecords} from '@/api/order'
import {Indicator} from 'mint-ui'

export default {
  namespaced: true,
  state: {
    userId: '', // 被查方的用户id
    userNick: '', // 被查方的用户昵称
    list: [], // 与被查房的往来记录
    loading: true // 正在加载中
  },

  mutations: {
    // 设置 userId
    SET_USER_ID: (state, userId) => {
      state.userId = userId
    },
    // 设置 用户昵称
    SET_USER_NICK: (state, userNick) => {
      state.userNick = userNick // string
    },
    // 设置 记录
    SET_LIST: (state, list) => {
      state.list = list // array
    },
    // 设置loading
    SET_LOADING: (state, bool) => {
      state.loading = bool // 布尔
    }
  },

  getters: {},

  actions: {
    // 设置用户Id
    SetRecordsUserId ({commit}, userid) {
      commit('SET_USER_ID', userid)
    },
    // 获取记录
    QueryOrderRecords ({state, commit, rootState}) {
      return new Promise((resolve, reject) => {
        commit('SET_LOADING', true)
        const token = rootState.main.token
        const userId = state.userId
        Indicator.open()
        queryRecords({token, userId}).then(response => {
          const data = response.data
          commit('SET_LIST', data.list) // 记录
          commit('SET_LOADING', false) // 加载中
          Indicator.close()
          resolve()
        }).catch(error => {
          Indicator.close()
          commit('SET_LOADING', false)
          reject(error)
        })
      })
    },
    // 重置状态
    ResetOrderRecord ({commit}) {
      commit('SET_USER_ID', '')
      commit('SET_USER_NICK', '')
      commit('SET_LIST', [])
    }
  }
}
